/*
 * Copyright (c) 2008, Russell Garrett
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.moneydance.modules.features.portfolioallocation;

import com.moneydance.apps.md.controller.FeatureModule;
import com.moneydance.apps.md.controller.FeatureModuleContext;

public class Main extends FeatureModule {
	private AccountListWindow accountListWindow = null;
	private AllocationHomePageView hpv = null;

	public void init() {
		try { // Exceptions thrown from here will kill Moneydance.
			FeatureModuleContext context = getContext();
			Settings.instance().initialize(context, "portfolioallocation");

			context.registerFeature(this, "showconsole", null, getName());
			hpv = new AllocationHomePageView(this);
			context.registerHomePageView(this, hpv);
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
	/** Process an invokation of this module with the given URI */
	public void invoke(String uri) {
		String command = uri;
		// String parameters = "";
		int theIdx = uri.indexOf('?');
		if (theIdx >= 0) {
			command = uri.substring(0, theIdx);
			// parameters = uri.substring(theIdx + 1);
		} else {
			theIdx = uri.indexOf(':');
			if (theIdx >= 0) {
				command = uri.substring(0, theIdx);
			}
		}

		if (command.equals("showconsole")) {
			showConsole();
		}
	}

	public String getName() {
		return "Portfolio Allocation";
	}

	public AllocationHomePageView getHomePageView() {
		return hpv;
	}
	

	public void cleanup() {
		closeConsole();
		ClassifiedSecurity.reset();
	}

	private synchronized void showConsole() {
		if (accountListWindow == null) {
			accountListWindow = new AccountListWindow(this);
			accountListWindow.setVisible(true);
		} else {
			accountListWindow.setVisible(true);
			accountListWindow.toFront();
			accountListWindow.requestFocus();
		}
	}

	FeatureModuleContext getUnprotectedContext() {
		return getContext();
	}

	synchronized void closeConsole() {
		if (accountListWindow != null) {
			accountListWindow.goAway();
			accountListWindow = null;
			System.gc();
		}
	}
}
